package com.tulingxueyuan.mall.modules.oms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tulingxueyuan.mall.common.api.ResultCode;
import com.tulingxueyuan.mall.common.exception.Asserts;
import com.tulingxueyuan.mall.dto.AddCarDto;
import com.tulingxueyuan.mall.modules.oms.mapper.OmsCartItemMapper;
import com.tulingxueyuan.mall.modules.oms.model.OmsCartItem;
import com.tulingxueyuan.mall.modules.oms.service.OmsCartItemService;
import com.tulingxueyuan.mall.modules.pms.model.PmsProduct;
import com.tulingxueyuan.mall.modules.pms.model.PmsSkuStock;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductService;
import com.tulingxueyuan.mall.modules.pms.service.PmsSkuStockService;
import com.tulingxueyuan.mall.modules.ums.model.UmsMember;
import com.tulingxueyuan.mall.modules.ums.service.UmsMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 购物车表 服务实现类
 * </p>
 *
 * @author SiXiaoLong
 * @since 2022-10-20
 */
@Service
public class OmsCartItemServiceImpl extends ServiceImpl<OmsCartItemMapper, OmsCartItem> implements OmsCartItemService {
    @Autowired
    UmsMemberService memberService;
    @Autowired
    PmsSkuStockService skuStockService;
    @Autowired
    PmsProductService productService;
    /**
     * 添加购物车
     */
    @Override
    public Boolean add(AddCarDto addCarDto) {
        OmsCartItem cartItem = new OmsCartItem();
        BeanUtils.copyProperties(addCarDto,cartItem);
        UmsMember umsMember = memberService.getCurrentMember();
        OmsCartItem item = getCatItem(addCarDto,umsMember.getId());
//        同一个商品，同一个用户，同一个sku下没有   则进行新增  反之则进行修改 数量和修改时间
        if (item==null){
            cartItem.setMemberId(umsMember.getId());
            cartItem.setMemberNickname(umsMember.getNickname());
    //        不能根据前端给的价格来查  需要根据传入的sku查
            PmsSkuStock skuStock = skuStockService.getById(cartItem.getProductSkuId());
            if (skuStock==null) Asserts.fail(ResultCode.VALIDATE_FAILED);
            cartItem.setPrice(skuStock.getPrice());
            cartItem.setSp1(skuStock.getSp1());
            cartItem.setSp2(skuStock.getSp2());
            cartItem.setSp3(skuStock.getSp3());
            cartItem.setProductPic(skuStock.getPic());
    //        根据商品
            PmsProduct product = productService.getById(cartItem.getProductId());
            if (product==null) Asserts.fail(ResultCode.VALIDATE_FAILED);
            cartItem.setProductName(product.getName());
            cartItem.setProductBrand(product.getBrandName());
            cartItem.setProductSn(product.getProductSn());
            cartItem.setProductSubTitle(product.getSubTitle());
            cartItem.setProductCategoryId(product.getProductCategoryId());
            Date currentTime = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = formatter.format(currentTime);
            ParsePosition pos = new ParsePosition(8);
            Date currentTime_2 = formatter.parse(dateString, pos);
            cartItem.setCreateDate(currentTime_2);
            cartItem.setModifyDate(currentTime_2);
            boolean save = this.save(cartItem);
            return save;
        }else {
            Date currentTime = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = formatter.format(currentTime);
            ParsePosition pos = new ParsePosition(8);
            Date currentTime_2 = formatter.parse(dateString, pos);
            LambdaUpdateWrapper<OmsCartItem> luw = new LambdaUpdateWrapper<>();
            luw.set(OmsCartItem::getQuantity,item.getQuantity()+1);
            luw.set(OmsCartItem::getModifyDate,currentTime_2);
            luw.eq(OmsCartItem::getId,item.getId());
            boolean update = this.update(luw);
            return update;
        }
//        LambdaQueryWrapper<OmsCartItem> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(OmsCartItem::getMemberId,cartItem.getMemberId());
//        lqw.eq(OmsCartItem::getProductId,cartItem.getProductId());
//        lqw.eq(OmsCartItem::getProductSkuId,cartItem.getProductSkuId());
//        OmsCartItem item = this.getOne(lqw);
//        OmsCartItem item = getCatItem(cartItem);
//        if (item!=null){
//            item.setQuantity(item.getQuantity()+1);
//            boolean save = this.updateById(item);
//            return save;
//        }else{
//            boolean save = this.save(cartItem);
//            return save;
//        }
    }
    /**
     * 修改购物车数量
     * @return
     */
    @Override
    public boolean updateQuantity(OmsCartItem cartItem) {
        UmsMember currentMember = memberService.getCurrentMember();
        LambdaUpdateWrapper<OmsCartItem> luw = new LambdaUpdateWrapper<>();
        luw.set(OmsCartItem::getQuantity,cartItem.getQuantity());
        luw.eq(OmsCartItem::getMemberId,currentMember.getId());
        luw.eq(OmsCartItem::getId,cartItem.getId());
        boolean update = this.update(luw);
        return update;
    }
    /**
     * 删除购物车商品
     */
    @Override
    public Boolean deleteCar(String ids ) {
//        LambdaQueryWrapper<OmsCartItem> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(OmsCartItem::getId,ids);
        boolean b = this.removeById(ids);
        return b;
    }

    /**
     * 判断同一个商品，同一个用户，同一个sku下是否添加重复的购物车
     */
    private OmsCartItem getCatItem(AddCarDto addCarDto,Long memberId){
        LambdaQueryWrapper<OmsCartItem> lqw = new LambdaQueryWrapper<>();
        lqw.eq(OmsCartItem::getMemberId,memberId);
        lqw.eq(OmsCartItem::getProductId,addCarDto.getProductId());
        lqw.eq(OmsCartItem::getProductSkuId,addCarDto.getProductSkuId());
        return this.getOne(lqw);
    }
}
